from django.shortcuts import render
from django.http import HttpResponse

from .tasks import test_build_job
from celery.result import AsyncResult


# Create your views here.
def index(request):
    result = test_build_job.delay(4, 4)
    request.session['task_id'] = str(result)
    return HttpResponse('task create')


def task_info(request):
    if 'task_id' in request.session:
        result = AsyncResult(request.session['task_id'])
        # result.result 返回执行结果
        if result.state != "PENDING": # PENDING 任务进行中 FAILURE 任务失败 SUCCESS 任务成功
            request.session.pop('task_id')
        return HttpResponse(result.state)
    return index(request)
